package propcalc_lang.ast;

import java.util.Map;
import java.util.Random;
import java.util.Set;

/** Un valor de verdad constante.
 *  @author Leonardo Val 
 */
public class TruthValue extends Proposition {
	public final boolean value; 
	
	public TruthValue(boolean value) {
		this.value = value;
	}

	@Override public String unparse() {
		return value ? "true" : "false";
	}

	public static TruthValue generate(Random random, int min, int max) {
		return new TruthValue(random.nextInt(2) > 0);
	}
	
	@Override public Set<String> variables(Set<String> vars) {
		return vars;
	}
	
	@Override public Proposition substitution(Map<String, Proposition> replacements) {
		return new TruthValue(value);
	}
	
	@Override public boolean match(Proposition pattern, Map<String, Proposition> replacements) {
		if (pattern instanceof Variable) {
			Variable var = (Variable) pattern;
			if (replacements.containsKey(var.identifier)) {
				return this.equals(replacements.get(var.identifier));
			} else {
				replacements.put(var.identifier, this);
				return true;
			}
		}
		return this.equals(pattern);
	}
	
	@Override public String toString() {
		return "TruthValue("+ (value ? "true" : "false") +")";
	}

	@Override public int hashCode() {
		return 31 + (value ? 1231 : 1237);
	}

	@Override public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		TruthValue other = (TruthValue) obj;
		if (value != other.value)
			return false;
		return true;
	}

}
